package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import org.springframework.beans.factory.annotation.Autowired;

import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalErrorConsts;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.util.StrTools;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInstFunc;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;

/**
 *
 */
public class WhitelistAttrHandler<I extends KiteInst, D extends AttrData>
    extends AbstractAttrHandler<KiteInst, AttrData> {
    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Autowired
    IQuerySceneInstService sceneInstQueryService;

    @Override
    public void before(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        // 2104051000211000订单审核页因为报错,页面都进不去了.
        KiteSceneInst sceneInst = sceneInstQueryService.getSceneInst(data.getSceneInstId());
        if (!KiteObjectUtils.isEmpty(sceneInst) && KiteStringUtils
            .isEqual(LocalKeyConsts.ASK_SOURCE_ORDER, sceneInst.getAskSource())) {
            return;
        }
        String sceneValue = null;
        KiteProdInstFunc prodInstFunc = ((KiteProdInstFunc) inst);
        KiteProdInst accProdInst = this.querySceneInstService.getProdInst(data.getCustId(), prodInstFunc.getAccProdInstId());
        if (!KiteObjectUtils.isEmpty(accProdInst)) {
            // 业务场景属性值
            sceneValue = accProdInst.getAttrValue("200001679");
        }

        if ("200003695".equals(attr.getFieldName())) {
            if (KiteStringUtils.equals(sceneValue, LocalKeyConsts.SCENE_B2B) && StrTools.isNotEmpty(value) && value.split(",").length > Integer.valueOf(LocalKeyConsts.WHITE_LIST_PHONE_NUM_100)) {
                LocalErrorConsts.UPLOAD_FILE_ERR_602.throwOut(LocalKeyConsts.WHITE_LIST_PHONE_NUM_100);
            }
            else if (KiteStringUtils.equals(sceneValue, LocalKeyConsts.SCENE_B2B2C) && StrTools.isNotEmpty(value) && value.split(",").length > Integer.valueOf(LocalKeyConsts.WHITE_LIST_PHONE_NUM_10)) {
                LocalErrorConsts.UPLOAD_FILE_ERR_602.throwOut(LocalKeyConsts.WHITE_LIST_PHONE_NUM_10);
            }
        }
        else if ("200003692".equals(attr.getFieldName())) {
            if (KiteStringUtils.equals(sceneValue, LocalKeyConsts.SCENE_B2B) && StrTools.isNotEmpty(value) && Integer.valueOf(value) > Integer.valueOf(LocalKeyConsts.SINGLE_CARD_OUTGOING_20)) {
                LocalErrorConsts.UPLOAD_FILE_ERR_603.throwOut(LocalKeyConsts.SINGLE_CARD_OUTGOING_20);
            }
            else if (KiteStringUtils.equals(sceneValue, LocalKeyConsts.SCENE_B2B2C) && StrTools.isNotEmpty(value) && Integer.valueOf(value) > Integer.valueOf(LocalKeyConsts.SINGLE_CARD_OUTGOING_100)) {
                LocalErrorConsts.UPLOAD_FILE_ERR_603.throwOut(LocalKeyConsts.SINGLE_CARD_OUTGOING_100);
            }
        }
    }

    @Override
    public void init(KiteAttr attr, KiteInst inst) {
        //语音MO(呼出)使用限额(分钟/月)
        if ("200003692".equals(attr.getFieldName())){
            attr.setOrderId("9");
            super.init(attr, inst);
        }

    }
}
